<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="pid">
      <el-input v-model="dataForm.pid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="type">
      <el-input v-model="dataForm.type" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="title">
      <el-input v-model="dataForm.title" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="权限编码[只有type=permission才有 user:view]" prop="percode">
      <el-input v-model="dataForm.percode" placeholder="权限编码[只有type=permission才有 user:view]"></el-input>
    </el-form-item>
    <el-form-item label="" prop="icon">
      <el-input v-model="dataForm.icon" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="href">
      <el-input v-model="dataForm.href" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="target">
      <el-input v-model="dataForm.target" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="open">
      <el-input v-model="dataForm.open" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="ordernum">
      <el-input v-model="dataForm.ordernum" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="是否可用[0不可用，1可用]" prop="available">
      <el-input v-model="dataForm.available" placeholder="是否可用[0不可用，1可用]"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          pid: '',
          type: '',
          title: '',
          percode: '',
          icon: '',
          href: '',
          target: '',
          open: '',
          ordernum: '',
          available: ''
        },
        dataRule: {
          pid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          percode: [
            { required: true, message: '权限编码[只有type=permission才有 user:view]不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          href: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          target: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          open: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          ordernum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          available: [
            { required: true, message: '是否可用[0不可用，1可用]不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/permission/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pid = data.permission.pid
                this.dataForm.type = data.permission.type
                this.dataForm.title = data.permission.title
                this.dataForm.percode = data.permission.percode
                this.dataForm.icon = data.permission.icon
                this.dataForm.href = data.permission.href
                this.dataForm.target = data.permission.target
                this.dataForm.open = data.permission.open
                this.dataForm.ordernum = data.permission.ordernum
                this.dataForm.available = data.permission.available
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/permission/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'pid': this.dataForm.pid,
                'type': this.dataForm.type,
                'title': this.dataForm.title,
                'percode': this.dataForm.percode,
                'icon': this.dataForm.icon,
                'href': this.dataForm.href,
                'target': this.dataForm.target,
                'open': this.dataForm.open,
                'ordernum': this.dataForm.ordernum,
                'available': this.dataForm.available
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
